{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cd4f8056-2015-4c28-8974-d9862db07e84",
   "metadata": {},
   "source": [
    "Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b8e5368-6268-4da9-9e8d-5c38637da8a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "def get_api_key():\n",
    "    api_key = os.getenv(\"UNS_API_KEY\")\n",
    "    if api_key is None:\n",
    "        raise ValueError(\"\"\"UNS_API_KEY environment variable not set. \n",
    "Set it in your current shell session with `export UNS_API_KEY=<api_key>`\"\"\")\n",
    "    return api_key"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11822c83-0791-432c-b1fb-05d8e2ae25bb",
   "metadata": {},
   "source": [
    "\"Usage\" instructions from README for `unstructured-python-client` (as of 01/29/2023)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c28a39c-ad38-47a5-8247-a2fa1488313c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from unstructured_client import UnstructuredClient\n",
    "from unstructured_client.models import shared\n",
    "from unstructured_client.models.errors import SDKError\n",
    "\n",
    "s = UnstructuredClient(api_key_auth=get_api_key())\n",
    "filename = \"../_sample_docs/layout-parser-paper-fast.pdf\"\n",
    "\n",
    "with open(filename, \"rb\") as f:\n",
    "    # Note that this currently only supports a single filea\n",
    "    files=shared.Files(\n",
    "        content=f.read(),\n",
    "        file_name=filename,\n",
    "\t)\n",
    "\n",
    "req = shared.PartitionParameters(\n",
    "    files=files,\n",
    "    # Other partition params\n",
    "    strategy='ocr_only',\n",
    "    languages=[\"eng\"],\n",
    ")\n",
    "\n",
    "try:\n",
    "    resp = s.general.partition(req)\n",
    "    print(resp.elements[0])\n",
    "except SDKError as e:\n",
    "    print(e)\n",
    "\n",
    "# {\n",
    "# 'type': 'UncategorizedText', \n",
    "# 'element_id': 'fc550084fda1e008e07a0356894f5816', \n",
    "# 'metadata': {\n",
    "#   'filename': 'layout-parser-paper-fast.pdf', \n",
    "#   'filetype': 'application/pdf', \n",
    "#   'languages': ['eng'], \n",
    "#   'page_number': 1\n",
    "#   }\n",
    "# }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5dfdb68-ba5d-4d21-98b2-4efe04126b7a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
